package com.hurl.waguji.biz.download;

import java.util.ArrayList;
import java.util.List;

import org.testng.log4testng.Logger;

import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.Formatter;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjCwXjlbThsBO;
import com.hurl.waguji.persistence.JcsjCwXjlbThsBOId;

class CwXjlbThsProcessor extends BaseDownloadForUpdateProcessor {
	public CwXjlbThsProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
	}
	private static final Logger log = Logger.getLogger(CwXjlbThsProcessor.class);

	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {

		if(StringUtils.isBlank(html)){
			return;
		}
		
		String gpdm = context.getEnvAsString("gpdm");

		
		String table = StringUtils.substringBetween(html, "<table","</table>");
		if(StringUtils.isBlank(table)){
			return ;
		}
		
		List<String> trlist = StringUtils.substringsBetween(table, "<tr", "</tr>");
		if(trlist==null || trlist.isEmpty()){
			return ;
		}
		List<List<String>> list = new ArrayList<List<String>>();
		List<String> list2 = null;
		List<String> tdlist = null;
		for(String tr : trlist){
			list2 = new ArrayList<String>();
			tdlist = StringUtils.substringsBetween(tr, "<td", "</td>");
			if(tdlist==null || tdlist.size()<=1){
				return ;
			}
			for(int i=1;i<tdlist.size();i++){
				String td = StringUtils.substringBetween(tdlist.get(i), "<span>", "</span>");
				list2.add(td);
			}	
			list.add(list2);
		}
		int size = 0;
		for(int i=0;i<list.size();i++){
			size+=list.get(i).size();
		}
		if(size==0){
			return;
		}
		for(int i=0;i<list.size();i++){
			if(size!=list.get(i).size()*list.size()){
				log.error("数据格式不对！");
				return;
			}
		}
		
		JcsjCwXjlbThsBO bo = null;
		for(int i=0;i<list.get(0).size();i++){
			bo = new JcsjCwXjlbThsBO();
			bo.setId(new JcsjCwXjlbThsBOId(gpdm,Formatter.toDate(list.get(0).get(i))));
			bo.setJyhdxjlr(Formatter.multiple10000(list.get(1).get(i)));
			bo.setJyhdxjlc(Formatter.multiple10000(list.get(2).get(i)));
			bo.setJyhdxjllje(Formatter.multiple10000(list.get(3).get(i)));
			bo.setTzhdxjlr(Formatter.multiple10000(list.get(4).get(i)));
			bo.setTzhdxjlc(Formatter.multiple10000(list.get(5).get(i)));
			bo.setTzhdxjllje(Formatter.multiple10000(list.get(6).get(i)));
			bo.setCzhdxjlr(Formatter.multiple10000(list.get(7).get(i)));
			bo.setCzhdxjlc(Formatter.multiple10000(list.get(8).get(i)));
			bo.setCzhdxjllje(Formatter.multiple10000(list.get(9).get(i)));
			bo.setHlbdyx(Formatter.multiple10000(list.get(10).get(i)));
			bo.setXjjdjwjzje(Formatter.multiple10000(list.get(11).get(i)));
			bo.setLastupdate(currentTime);
			
			collector.addBO(bo);
		}
	}


	@Override
	public UrlPattern getUrlPattern() {
		return new StockIteratorUrlPattern(context) {
			
			@Override
			public String bo2url(DmGpBO bo) {
				return "http://basic.10jqka.com.cn/"+bo.getGpDmJ()+"/cwtable/Cash_declaredate.html";
			}
		};
	}

}
